package com.bycx.auto;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.Map;
import java.util.Properties;

public class GenCodeUtil
{
	private String tablename;
	private String schema;
	private String basePackagePath = "com.bycx.auto";
	private String entityPath;
	private String mapperPath;
	private String servicePath;
	private String actionPath;
	private String actionRequestMappingValue;
	private String actionFileName;
	private String jspPath;
	private String jsPath;
	private String URL;
	private String NAME;
	private String PASS;
	private String DRIVER;
	private String pks = "";
	private String[] colnames;
	private String[] colTypes;
	private int[] colSizes;
	private int[] colNullables;
	private String[] remarks;
	private String entityName;

	public void genEntity()
	{
		OutFile(parseJavaBean(), this.entityName + ".java", this.entityPath, "");
	}
	public void genService() {
		OutFile(parseService(), "I" + this.entityName + "Service.java", this.servicePath, "");
		if ((this.servicePath == null) || ("".equals(this.servicePath)))
			OutFile(parseServiceImpl(), this.entityName + "ServiceImpl.java", this.servicePath, "");
		else
			OutFile(parseServiceImpl(), this.entityName + "ServiceImpl.java", this.servicePath + ".impl", "");
	}

	public void genMapper()
	{
		OutFile(parseMapper(), this.entityName + "Mapper.java", this.mapperPath, "");

		if (this.DRIVER.contains("mysql"))
			OutFile(parseXML(), this.entityName + "-mapper.mysql.xml", this.mapperPath, "");
		else if (this.DRIVER.contains("oracle"))
			OutFile(parseXML(), this.entityName + "-mapper.oracle.xml", this.mapperPath, "");
	}

	public void genController() {
		OutFile(parseAction(), this.actionFileName + ".java", this.actionPath, "");
	}
	public void genJsp() {
		OutFile(parseJsp(), this.entityName.toLowerCase() + ".index.jsp", this.jspPath, "jsp");
		OutFile(parseJsp(), this.entityName.toLowerCase() + ".form.jsp", this.jspPath, "jsp");
	}
	public void genJs() {
		OutFile(parseJs(), this.entityName.toLowerCase() + ".index.js", this.jsPath, "js");
		OutFile(parseJs(), this.entityName.toLowerCase() + ".form.js", this.jsPath, "js");
	}

	public GenCodeUtil(Map<String, String> map) {
		this.URL = ((String)map.get("URL"));
		this.NAME = ((String)map.get("NAME"));
		this.PASS = ((String)map.get("PASS"));
		this.DRIVER = ((String)map.get("DRIVER"));

		this.tablename = ((String)map.get("tablename"));
		this.schema = ((String)map.get("schema"));
		this.entityPath = ((String)map.get("entityPath"));
		this.mapperPath = ((String)map.get("mapperPath"));
		this.servicePath = ((String)map.get("servicePath"));
		this.actionPath = ((String)map.get("controllerPath"));
		this.actionRequestMappingValue = ((String)map.get("controllerRequestMappingValue"));
		this.actionFileName = ((map.get("controllerFileName") == null) || ("".equals(((String)map.get("controllerFileName")).trim())) ? getEntityName(this.tablename) + "Controller" : (String)map.get("controllerFileName"));
		this.jspPath = ((String)map.get("jspPath"));
		this.jsPath = ((String)map.get("jsPath"));

		if ((this.entityPath == null) || ("".equals(this.entityPath))) {
			this.entityPath = this.basePackagePath;
		}
		if ((this.mapperPath == null) || ("".equals(this.mapperPath))) {
			this.mapperPath = this.basePackagePath;
		}
		if ((this.servicePath == null) || ("".equals(this.servicePath))) {
			this.servicePath = this.basePackagePath;
		}
		if ((this.actionPath == null) || ("".equals(this.actionPath))) {
			this.actionPath = this.basePackagePath;
		}
		if ((this.jspPath == null) || ("".equals(this.jspPath))) {
			this.jspPath = this.basePackagePath;
		}
		if ((this.jsPath == null) || ("".equals(this.jsPath))) {
			this.jsPath = this.basePackagePath;
		}

		Connection con = null;
		this.tablename = this.tablename.toUpperCase();
		String sql = "select * from " + this.tablename;
		Statement pStemt = null;
		try {
			try {
				Class.forName(this.DRIVER);
			} catch (ClassNotFoundException e1) {
				e1.printStackTrace();
			}

			Properties props = new Properties();
			props.put("user", this.NAME);
			props.put("password", this.PASS);
			props.put("remarksReporting", "true");
			con = DriverManager.getConnection(this.URL, props);

			pStemt = con.createStatement();
			ResultSet rs = pStemt.executeQuery(sql);
			ResultSetMetaData rsmd = rs.getMetaData();

			ResultSet r = con.getMetaData().getPrimaryKeys(null, this.schema, this.tablename);
			while (r.next()) {
				this.pks = (this.pks + r.getString("COLUMN_NAME") + ",");
			}
			if (this.pks == "") {
				System.out.println("警告:该表没有主键！");
			}
			int size = rsmd.getColumnCount();
			this.colnames = new String[size];
			this.colTypes = new String[size];
			this.colSizes = new int[size];
			this.colNullables = new int[size];
			for (int i = 0; i < size; i++) {
				this.colnames[i] = rsmd.getColumnName(i + 1);
				this.colTypes[i] = rsmd.getColumnTypeName(i + 1);
				this.colSizes[i] = rsmd.getColumnDisplaySize(i + 1);
				this.colNullables[i] = rsmd.isNullable(i + 1);
			}
			this.entityName = getEntityName(this.tablename);

			ResultSet rr = null;
			if (this.DRIVER.contains("oracle")) {
				if ((this.schema == null) || ("".equals(this.schema.trim()))) {
					System.out.println("oracle请填上schema，否则不能生成注释！");
				}
				rr = con.getMetaData().getColumns(null, this.schema, this.tablename, "%");
			} else if (this.DRIVER.contains("mysql")) {
				rr = con.getMetaData().getColumns(null, "%", this.tablename, "%");
			}
			this.remarks = new String[size];
			int i = 0;
			while (rr.next()) {
				this.remarks[i] = rr.getString("REMARKS");
				i++;
			}

		}
		catch (SQLException e)
		{
			e.printStackTrace();
			try
			{
				con.close();
			} catch (SQLException e1) {
				e.printStackTrace();
			}
		}
		finally
		{
			try
			{
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	private void OutFile(String content, String fileName, String path, String type)
	{
		PrintWriter pw = null;
		try {
			File directory = new File("");

			String outputPath = "";
			if ("jsp".equals(type))
				outputPath = directory.getAbsolutePath() + "/WebContent/WEB-INF/jsp/" + path.replace(".", "/") + "/" + fileName;
			else if ("js".equals(type))
				outputPath = directory.getAbsolutePath() + "/WebContent/assets/business/" + path.replace(".", "/") + "/" + fileName;
			else {
				outputPath = directory.getAbsolutePath() + "/src/main/java/" + path.replace(".", "/") + "/" + fileName;
			}
			pw = new PrintWriter(new FileWriter(outputPath));
			pw.println(content);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (pw != null) {
				pw.flush();
				pw.close();
			}
		}
	}

	private String parseJavaBean() {
		StringBuffer sb = new StringBuffer();
		sb.append("package " + this.entityPath + ";\r\n");
		sb.append("\r\n");
		sb.append("import java.util.Date;\r\n");
		sb.append("import java.math.BigDecimal;\r\n");

		if ((this.schema == null) || ("".equals(this.schema.trim())))
			sb.append("//" + this.tablename + "\r\n");
		else {
			sb.append("//" + this.tablename + ",schema=" + this.schema + "\r\n");
		}
		sb.append("public class " + this.entityName + " implements java.io.Serializable{\r\n");
		processAllAttrs(sb);
		processAllMethod(sb);
		sb.append("}\r\n");

		return sb.toString();
	}

	private String parseMapper() {
		StringBuffer sb = new StringBuffer();
		sb.append("package " + this.mapperPath + ";\r\n");

		sb.append("import com.bycx.core.mapper.BaseMapper;\r\n");
		sb.append("import " + this.entityPath + "." + this.entityName + ";\r\n");
		sb.append("\r\n");

		sb.append("public interface " + this.entityName + "Mapper extends BaseMapper<" + this.entityName + ">{\r\n");

		sb.append("}\r\n");
		return sb.toString();
	}

	private String parseService() {
		StringBuffer sb = new StringBuffer();
		sb.append("package " + this.servicePath + ";\r\n");

		sb.append("import com.bycx.core.service.IBaseService;\r\n");
		sb.append("import " + this.entityPath + "." + this.entityName + ";\r\n");
		sb.append("\r\n");
		sb.append("public interface I" + this.entityName + "Service extends IBaseService<" + this.entityName + ">{\r\n");

		sb.append("}\r\n");
		return sb.toString();
	}

	private String parseServiceImpl() {
		StringBuffer sb = new StringBuffer();
		sb.append("package " + this.servicePath + ".impl;\r\n");

		sb.append("import org.springframework.beans.factory.annotation.Autowired;\r\n");
		sb.append("import org.springframework.stereotype.Service;\r\n");
		sb.append("import com.bycx.core.service.BaseService;\r\n");
		sb.append("import " + this.entityPath + "." + this.entityName + ";\r\n");
		sb.append("import " + this.mapperPath + "." + this.entityName + "Mapper;\r\n");
		sb.append("import " + this.servicePath + ".I" + this.entityName + "Service;\r\n");
		sb.append("\r\n");
		sb.append("@Service\r\n");
		sb.append("public class " + this.entityName + "ServiceImpl extends BaseService<" + this.entityName + "> implements I" + this.entityName + "Service{\r\n");
		sb.append("\t@Autowired\r\n");
		String mapperVar = this.entityName.substring(0, 1).toLowerCase() + this.entityName.substring(1);
		sb.append("\tprivate " + this.entityName + "Mapper " + mapperVar + "Mapper;\r\n");
		sb.append("\t@Autowired\r\n");
		sb.append("\tpublic void setBaseMapper(){super.setBaseMapper(" + mapperVar + "Mapper);}\r\n\n");

		sb.append("}\r\n");
		return sb.toString();
	}

	private String parseXML() {
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
		sb.append("<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">\r\n");
		sb.append("\r\n");
		sb.append("<mapper namespace=\"" + this.mapperPath + "." + this.entityName + "Mapper\">\r\n");

		sb.append("\t<sql id=\"column\">\r\n");
		for (int i = 0; i < this.colnames.length; i++) {
			sb.append("\t\tt." + this.colnames[i] + " " + getAttrName(this.colnames[i]) + ",\r\n");
		}
		sb.deleteCharAt(sb.lastIndexOf(","));
		sb.append("\t</sql>\r\n");

		sb.append("\t<sql id=\"condition\">\r\n");
		for (int i = 0; i < this.colnames.length; i++) {
			sb.append("\t\t\t<if test=\"" + getAttrName(this.colnames[i]) + " != null and " + getAttrName(this.colnames[i]) + " != ''\">");
			sb.append("AND t." + this.colnames[i] + " = #{" + getAttrName(this.colnames[i]) + "}</if>\r\n");
		}
		sb.append("\t</sql>\r\n");

		sb.append("\t<select id=\"get\" parameterType=\"java.util.Map\" resultType=\"" + this.entityPath + "." + this.entityName + "\">\r\n");
		sb.append("\t\tSELECT\r\n");
		sb.append("\t\t\t<include refid=\"column\"/>\r\n");
		if ((this.schema == null) || ("".equals(this.schema.trim())))
			sb.append("\t\tFROM " + this.tablename + " t\r\n");
		else {
			sb.append("\t\tFROM " + this.schema + "." + this.tablename + " t\r\n");
		}
		sb.append("\t\t<trim prefix=\"WHERE\" prefixOverrides=\"AND |OR \">\r\n");

		sb.append("\t\t\t<include refid=\"condition\"/>\r\n");
		sb.append("\t\t</trim>\r\n");
		sb.append("\t</select>\r\n");

		sb.append("\t<select id=\"getList\" parameterType=\"java.util.Map\" resultType=\"" + this.entityPath + "." + this.entityName + "\">\r\n");
		sb.append("\t\tSELECT\r\n");
		sb.append("\t\t\t<include refid=\"column\"/>\r\n");
		if ((this.schema == null) || ("".equals(this.schema.trim())))
			sb.append("\t\tFROM " + this.tablename + " t\r\n");
		else {
			sb.append("\t\tFROM " + this.schema + "." + this.tablename + " t\r\n");
		}
		sb.append("\t\t<trim prefix=\"WHERE\" prefixOverrides=\"AND |OR \">\r\n");

		sb.append("\t\t\t<include refid=\"condition\"/>\r\n");
		sb.append("\t\t</trim>\r\n");
		sb.append("\t</select>\r\n");

		sb.append("\t<insert id=\"insert\" parameterType=\"" + this.entityPath + "." + this.entityName + "\">\r\n");
		sb.append("\tinsert into " + this.tablename + "\r\n");
		sb.append("\t(\r\n");
		for (int i = 0; i < this.colnames.length; i++) {
			sb.append("\t\t" + this.colnames[i] + ",\r\n");
		}
		sb.deleteCharAt(sb.lastIndexOf(","));
		sb.append("\t)\r\n");
		sb.append("\tvalues\r\n");
		sb.append("\t(\r\n");
		for (int i = 0; i < this.colnames.length; i++) {
			sb.append("\t\t#{" + getAttrName(this.colnames[i]) + "},\r\n");
		}
		sb.deleteCharAt(sb.lastIndexOf(","));
		sb.append("\t)\r\n");
		sb.append("\t</insert>\r\n");

		sb.append("\t<update id=\"updateByPrimaryKey\" parameterType=\"" + this.entityPath + "." + this.entityName + "\">\r\n");
		sb.append("\t\tUPDATE " + this.tablename + "\r\n");
		sb.append("\t\t<trim prefix=\"SET\" suffixOverrides=\",\">\r\n");
		for (int i = 0; i < this.colnames.length; i++)
			if (!"ID".equals(this.colnames[i]))
			{
				sb.append("\t\t\t<if test=\"" + getAttrName(this.colnames[i]) + " != null\">\r\n");
				sb.append("\t\t\t\t" + this.colnames[i] + " = #{" + getAttrName(this.colnames[i]) + "},\r\n");
				sb.append("\t\t\t</if>\r\n");
			}
		sb.append("\t\t</trim>\r\n");
		String[] pksArr = this.pks.split(",");
		for (int i = 0; i < pksArr.length; i++) {
			if (i == 0)
				sb.append("\t\tWHERE ");
			else {
				sb.append(" AND ");
			}
			sb.append(pksArr[i] + "=#{" + getAttrName(pksArr[i]) + "}");
		}
		sb.append("\r\n");
		sb.append("\t</update>\r\n");

		sb.append("\t<delete id=\"deleteByPrimaryKey\" parameterType=\"java.lang.String\">\r\n");
		sb.append("\t\tDELETE FROM " + this.tablename);
		for (int i = 0; i < pksArr.length; i++) {
			if (i == 0)
				sb.append(" WHERE ");
			else {
				sb.append(" AND ");
			}
			sb.append(pksArr[i] + "=#{" + getAttrName(pksArr[i]) + "}");
		}
		sb.append("\r\n");
		sb.append("\t</delete>\r\n");

		sb.append("\t<delete id=\"delete\" parameterType=\"java.util.List\">\r\n");
		sb.append("\t\tDELETE FROM " + this.tablename + " WHERE ID IN\r\n");
		sb.append("\t\t<foreach collection=\"list\" item=\"id\" open=\"(\" separator=\",\" close=\")\">\r\n");
		sb.append("\t\t\t#{id}\r\n");
		sb.append("\t\t</foreach>\r\n");
		sb.append("\t</delete>\r\n");

		sb.append("\t<delete id=\"deleteObj\" parameterType=\"java.util.List\">\r\n");
		sb.append("\t\tDELETE FROM " + this.tablename + " WHERE ID IN\r\n");
		sb.append("\t\t<foreach collection=\"list\" item=\"obj\" open=\"(\" separator=\",\" close=\")\">\r\n");
		sb.append("\t\t\t#{obj.id}\r\n");
		sb.append("\t\t</foreach>\r\n");
		sb.append("\t</delete>\r\n");

		sb.append("\t<select id=\"getByPrimaryKey\" parameterType=\"java.lang.String\" resultType=\"" + this.entityPath + "." + this.entityName + "\">\r\n");
		sb.append("\t\tSELECT\r\n");
		sb.append("\t\t\t<include refid=\"column\"/>\r\n");
		if ((this.schema == null) || ("".equals(this.schema.trim())))
			sb.append("\t\tFROM " + this.tablename + " t\r\n");
		else {
			sb.append("\t\tFROM " + this.schema + "." + this.tablename + " t\r\n");
		}
		for (int i = 0; i < pksArr.length; i++) {
			if (i == 0)
				sb.append("\t\tWHERE ");
			else {
				sb.append(" AND ");
			}
			sb.append(pksArr[i] + "=#{" + getAttrName(pksArr[i]) + "}");
		}
		sb.append("\r\n");
		sb.append("\t</select>\r\n");

		sb.append("</mapper>\r\n");
		sb.append("\r\n");
		return sb.toString();
	}

	private String parseAction() {
		StringBuffer sb = new StringBuffer();
		sb.append("package " + this.actionPath + ";\r\n");
		sb.append("import java.util.List;\r\n");
		sb.append("import org.springframework.beans.factory.annotation.Autowired;\r\n");
		sb.append("import org.springframework.stereotype.Controller;\r\n");
		sb.append("import org.springframework.web.bind.annotation.RequestMapping;\r\n");
		sb.append("import org.springframework.web.bind.annotation.RequestMethod;\r\n");
		sb.append("import org.springframework.web.bind.annotation.RequestParam;\r\n");
		sb.append("import org.springframework.web.bind.annotation.ResponseBody;\r\n");
		sb.append("import com.bycx.core.controller.BaseController;\r\n");
		sb.append("import com.bycx.core.util.Page;\r\n");
		sb.append("import " + this.entityPath + "." + this.entityName + ";\r\n");
		sb.append("import " + this.servicePath + ".I" + this.entityName + "Service;\r\n");
		sb.append("\r\n");
		sb.append("@Controller\r\n");
		sb.append("@RequestMapping(value=\"" + this.actionRequestMappingValue + "\")\r\n");
		sb.append("public class " + this.actionFileName + " extends BaseController {\r\n");
		sb.append("\t@Autowired\r\n");
		String entityNameLowercase = this.entityName.substring(0, 1).toLowerCase() + this.entityName.substring(1);
		sb.append("\tprivate I" + this.entityName + "Service " + entityNameLowercase + "Service;\r\n");

		sb.append("\r\n");
		sb.append("\t@RequestMapping(value=\"/index\", method=RequestMethod.GET)\r\n");
		sb.append("\tpublic String index(Page<?> page){\r\n");
		sb.append("\t\treturn \"/" + this.jspPath + "/" + entityNameLowercase.toLowerCase() + ".index\";\r\n");
		sb.append("\t}\r\n");

		sb.append("\r\n");
		sb.append("\t@ResponseBody\r\n");
		sb.append("\t@RequestMapping(value=\"/list\", method=RequestMethod.POST)\r\n");
		sb.append("\tpublic Page<?> list(Page<?> page) {\r\n");
		sb.append("\t\tList<" + this.entityName + "> list = " + entityNameLowercase + "Service.getList(page.getParams());\r\n");
		sb.append("\t\tpage.setList(list);\r\n");
		sb.append("\t\treturn page;\r\n");
		sb.append("\t}\r\n");

		sb.append("\r\n");
		sb.append("\t@RequestMapping(value=\"/form\", method=RequestMethod.GET)\r\n");
		sb.append("\tpublic String form(Page<?> page){\r\n");
		sb.append("\t\treturn \"/" + this.jspPath + "/" + entityNameLowercase.toLowerCase() + ".form\";\r\n");
		sb.append("\t}\r\n");

		sb.append("\r\n");
		sb.append("\t@ResponseBody\r\n");
		sb.append("\t@RequestMapping(value=\"/save\", method=RequestMethod.POST)\r\n");
		sb.append("\tpublic Page<?> save(Page<?> page," + this.entityName + " " + entityNameLowercase + "){\r\n");
		sb.append("\t\t" + entityNameLowercase + "Service.save(" + entityNameLowercase + ");\r\n");
		sb.append("\t\treturn page;\r\n");
		sb.append("\t}\r\n");

		sb.append("\r\n");
		sb.append("\t@ResponseBody\r\n");
		sb.append("\t@RequestMapping(value=\"/delete\", method=RequestMethod.POST)\r\n");
		sb.append("\tpublic Page<?> delete(Page<?> page, @RequestParam(\"ids[]\") List<String> ids) {\r\n");
		sb.append("\t\t" + entityNameLowercase + "Service.delete(ids);\r\n");
		sb.append("\t\treturn page;\r\n");
		sb.append("\t}\r\n");

		sb.append("}\r\n");
		return sb.toString();
	}
	private String parseJsp() {
		return "";
	}
	private String parseJs() {
		return "";
	}
	private void processAllAttrs(StringBuffer sb) {
		sb.append("\tprivate static final long serialVersionUID = 1L;\r\n");
		for (int i = 0; i < this.colnames.length; i++) {
			sb.append("\t/** " + this.remarks[i] + " */\r\n");
			sb.append("\tprivate " + sqlType2JavaType(this.colTypes[i]) + " " + getAttrName(this.colnames[i]) + ";\r\n");
		}
	}

	private void processAllMethod(StringBuffer sb)
	{
		for (int i = 0; i < this.colnames.length; i++)
		{
			sb.append("\tpublic " + sqlType2JavaType(this.colTypes[i]) + " get" + getEntityName(this.colnames[i]) + "(){\r\n");
			sb.append("\t\treturn " + getAttrName(this.colnames[i]) + ";\r\n");
			sb.append("\t}\r\n");

			sb.append("\tpublic void set" + getEntityName(this.colnames[i]) + "(" + sqlType2JavaType(this.colTypes[i]) + " " + getAttrName(this.colnames[i]) + "){\r\n");
			sb.append("\t\tthis." + getAttrName(this.colnames[i]) + "=" + getAttrName(this.colnames[i]) + ";\r\n");
			sb.append("\t}\r\n");
		}
	}

	private String initcap(String str)
	{
		str = str.toLowerCase();
		char[] ch = str.toCharArray();
		if ((ch[0] >= 'a') && (ch[0] <= 'z')) {
			ch[0] = ((char)(ch[0] - ' '));
		}

		return new String(ch);
	}

	private String sqlType2JavaType(String sqlType)
	{
		if ((sqlType.equalsIgnoreCase("binary_double")) || (sqlType.equalsIgnoreCase("double")))
			return "Double";
		if ((sqlType.equalsIgnoreCase("binary_float")) || (sqlType.equalsIgnoreCase("float")))
			return "float";
		if (sqlType.equalsIgnoreCase("blob"))
			return "byte[]";
		if ((sqlType.equalsIgnoreCase("int")) || (sqlType.equalsIgnoreCase("integer")) || (sqlType.equalsIgnoreCase("smallint")))
			return "Integer";
		if ((sqlType.equalsIgnoreCase("char")) || (sqlType.equalsIgnoreCase("nvarchar2")) || (sqlType.equalsIgnoreCase("varchar2")) || (sqlType.equalsIgnoreCase("varchar")))
			return "String";
		if ((sqlType.equalsIgnoreCase("date")) || (sqlType.equalsIgnoreCase("datetime")) || (sqlType.equalsIgnoreCase("timestamp")) || (sqlType.equalsIgnoreCase("timestamp with local time zone")) || (sqlType.equalsIgnoreCase("timestamp with time zone")))
			return "Date";
		if (sqlType.equalsIgnoreCase("number"))
			return "Double";
		if (sqlType.equalsIgnoreCase("bigint"))
			return "Long";
		if (sqlType.equalsIgnoreCase("decimal")) {
			return "BigDecimal";
		}

		return "未添加映射";
	}

	private String getEntityName(String tableName)
	{
		String entityName = "";
		String[] names = tableName.split("_");
		for (String n : names) {
			entityName = entityName + initcap(n);
		}
		return entityName;
	}

	private String getAttrName(String colName)
	{
		String attrName = "";
		String[] names = colName.split("_");
		for (int i = 0; i < names.length; i++) {
			if (i == 0)
				attrName = attrName + names[i].toLowerCase();
			else {
				attrName = attrName + initcap(names[i]);
			}
		}
		return attrName;
	}
}